package com.atguigu.guli.service.oss.controller;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.oss.service.FileService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.temporal.TemporalUnit;
import java.util.concurrent.TimeUnit;

@Slf4j
@Api(tags = "文件处理")
@RestController
@RequestMapping("admin/oss/file")
@CrossOrigin
public class FileController {

    @Autowired
    private FileService fileService;

    // 文件上传
    @PostMapping("uploadFile")
    // 参数1: 上传的文件表单项;     参数2: oss保存文件的目录(模块)
    public R uploadFile(MultipartFile file, String module) {
        String path = fileService.uploadFile(file, module);

        return R.ok().data("path", path);
    }

    // 文件删除
    @DeleteMapping("delFile")
    public R delFile(@RequestParam("path") String path, @RequestParam("module") String module) {

        // 睡眠2秒
/*        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        fileService.delFile(path, module);

        return R.ok();
    }
}
